package com.aube.plugin.figure;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.ImageSizeDefineUtil;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.SysUtil;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.List;

/**
 * Created by huyaonan on 16/5/23.
 */
public class HostOneLineView extends DLLayout implements IFigureView {

    private int mSpace=0;
    private int mItemWidth=0;
    private int mItemHeight=0;

    public HostOneLineView(Context context) {
        this(context, null);
    }
    public HostOneLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public HostOneLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mSpace = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        int width = SysUtil.getAbsoluteScreenWidth(context);
        mItemWidth = (width-4*mSpace)/3;
        mItemHeight = mItemWidth*4/3;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = mItemHeight + (getChildCount()-1) /3*(mItemHeight + mSpace);
        super.onMeasure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).measure(View.MeasureSpec.makeMeasureSpec(mItemWidth, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(mItemHeight, View.MeasureSpec.EXACTLY));
        }
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        for (int i = 0; i < getChildCount(); i++) {
            int x = i%3;
            int y = i/3;

            int left = x*(mItemWidth + mSpace) + mSpace;
            int top = y*(mItemHeight + mSpace);

            getChildAt(i).layout(left, top, left + mItemWidth, top + mItemHeight);
        }
    }

    public void anim(int delay) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount ; i++) {
            int x = i%3;
            int y = i/3;

            final View view = getChildAt(i);
            view.setAlpha(0f);
            final ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();

                    view.setAlpha(value);
                    view.setTranslationX(mItemWidth*(1-value));
                }
            });
            anim.setDuration(300);
            anim.setStartDelay(delay+200*(x+y));
            anim.start();
        }
    }

    @Override
    public void fillData(Object o) {
        if(o == null || !(o instanceof List))
            return;
        List list = (List) o;
        int size = list.size();
        for(int i=0; i<size; i++) {
            final Character character = (Character) list.get(i);
            final ImageView img = new ImageView(context);
            img.setScaleType(ImageView.ScaleType.CENTER_CROP);

            addView(img, new ViewGroup.LayoutParams(mItemWidth, mItemHeight));
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBridge != null && character.canClick()) {
                        DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
                        intent.putExtra(CharacterActivity.ACTOR, character);
                        mBridge.openPluginActivity(intent);
                    }
                }
            });
            img.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk_default));
            ImageLoader.getInstance(context).fillImage(img, ImageSizeDefineUtil.getImgUrl(character.actorPic, 400, 400));
        }
    }

    @Override
    public void notifyAnim(int animDelay) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
